<?php
namespace app\merchant\controller;
use app\common\controller\AdminBase;
use app\common\controller\Platurl;
use app\common\model\CardConsume;
use app\common\model\CardMember;
use app\common\model\CardPaycardlog;
use app\common\model\CardUserBonus;
use app\common\model\CardUserFee;
use app\common\model\CardUserGet;
use app\common\model\CardUserUpdate;
use app\common\model\LoginUsers;
use app\common\model\Merchant;
use app\common\model\Momerchant;
use app\common\model\OpenTemplate;
use app\common\model\Paylog;
use think\Controller;
use think\Request;
use think\Session;
use Wechat\Wxobjopen;

class Statis extends AdminBase
{
    //END
    //首页看板
    //会员总数
    public function member_num(Request $request){
        $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        //获取昨日起始时间戳和结束时间戳
//        $beginYesterday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
//        $endYesterday = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
        //获取本月起始时间戳和结束时间戳
//        $beginThismonth = mktime(0, 0, 0, date('m'), 1, date('Y'));
//        $endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        //获取本年度起始时间戳和结束时间戳
//        $begin_year = strtotime(date("Y", time()) . "-1" . "-1"); //本年开始
//        $end_year = strtotime(date("Y", time()) . "-12" . "-31"); //本年结束
        //获取近30天开始时间戳结束时间戳
//        $begin_30 = time() - (30 * 86400); //本年开始
//        $end_30 = time(); //本年结束
//        $beginWeek = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y'));
//        $endWeek = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y'));
        //END
        $CardUserGet = new CardUserGet();
        $top_mid = Session::get('login_top_mid');
        $map['top_mid'] = $top_mid;
        $map['card_type'] = 'MEMBER_CARD';
        $data['total_num'] = $CardUserGet->where($map)->count();    //会员总数
        //本周新增
        $map1['top_mid'] = $top_mid;
        $map1['card_type'] = 'MEMBER_CARD';
        $map1['create_time'] = array(array('egt', $beginToday), array('elt', $endToday), 'and');
        $data['rec_user_num'] = $CardUserGet->where($map1)->count();
        //END
        //男
        $map2['top_mid'] = $top_mid;
        $map2['sex'] = 1;
        $map2['card_type'] = 'MEMBER_CARD';
        $data['male_num'] = $CardUserGet->where($map2)->count();
        //女
        $map3['top_mid'] = $top_mid;
        $map3['sex'] = 0;
        $map3['card_type'] = 'MEMBER_CARD';
        $data['female_num'] = $CardUserGet->where($map3)->count();
        $re_param['return_code'] = "SUCCESS";
        $re_param['return_msg'] = "成功";
        $re_param['data']['item'] = $data;
        return json($re_param);
    }

    //余额
    public function statis_balance_num(Request $request){
        //END
        $CardUserFee = new CardUserFee();
        $CardMember = new CardMember();
        $CardUserGet = new CardUserGet();
        $top_mid = Session::get('login_top_mid');
        $map['top_mid'] = $top_mid;
        $cardinfo = $CardMember->where($map)->field('card_id')->find();
        if ($cardinfo) {
            //END
            $map1['card_id'] = $cardinfo['card_id'];
            $map1['type'] = ['in', [1, 999]];
//        $map1['cate'] = 1;
            $data['total_rec_num'] = $CardUserFee->where($map1)->sum('num');    //总充值额
            $re_param['total_rec_num_sql'] = $CardUserFee->getLastSql();
            //END
            $map2['card_id'] = $cardinfo['card_id'];
            $map2['type'] = 2;
//        $map2['cate'] = 2;
            $data['total_con_num'] = $CardUserFee->where($map2)->sum('num');    //总消费额
            $re_param['total_con_num_sql'] = $CardUserFee->getLastSql();
            //END
            $map3['card_id'] = $cardinfo['card_id'];
//        $map2['type'] = 2;
            $data['total_surplus_num'] = $CardUserGet->where($map3)->sum('balance');    //总剩余
        }else{
            $data['total_rec_num'] = 0;
            $data['total_con_num'] = 0;
            $data['total_surplus_num'] = 0;
        }
        //END
        $re_param['return_code'] = "SUCCESS";
        $re_param['return_msg'] = "成功";
        $re_param['data']['item'] = $data;
        return json($re_param);
    }

    //积分
    public function statis_bonus_num(Request $request){
        //END
        $CardUserBonus = new CardUserBonus();
        $CardMember = new CardMember();
        $CardUserGet = new CardUserGet();
        $top_mid = Session::get('login_top_mid');
        $map['top_mid'] = $top_mid;
        $cardinfo = $CardMember->where($map)->field('card_id')->find();
        if ($cardinfo) {
            //END
            $map1['card_id'] = $cardinfo['card_id'];
            $map1['type'] = ['in', [1, 999]];
//        $map1['cate'] = 1;
            $data['total_rec_num'] = $CardUserBonus->where($map1)->sum('num');    //总充值额
//            $re_param['total_rec_num_sql'] = $CardUserBonus->getLastSql();
            //END
            $map2['card_id'] = $cardinfo['card_id'];
            $map2['type'] = 2;
//        $map2['cate'] = 2;
            $data['total_con_num'] = $CardUserBonus->where($map2)->sum('num');    //总消费额
//            $re_param['total_con_num_sql'] = $CardUserBonus->getLastSql();
            //END
            $map3['card_id'] = $cardinfo['card_id'];
//        $map3['type'] = 2;
//            $data['total_surplus_num'] = $CardUserGet->where($map3)->sum('bonus');    //总剩余
            $data['total_surplus_num'] = $data['total_rec_num']-$data['total_con_num'];
//            $re_param['total_surplus_num_sql'] = $CardUserGet->getLastSql();
        }else{
            $data['total_rec_num'] = 0;
            $data['total_con_num'] = 0;
            $data['total_surplus_num'] = 0;
        }
        //END
        $re_param['return_code'] = "SUCCESS";
        $re_param['return_msg'] = "成功";
        $re_param['data']['item'] = $data;
        return json($re_param);
    }

    //消费统计
    public function statis_payfee(Request $request){
        if ($request->isAjax()) {
            //获取本月起始时间戳和结束时间戳
            $beginThismonth = mktime(0, 0, 0, date('m'), 1, date('Y'));
            $endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
            //获取本年度起始时间戳和结束时间戳
            $begin_year = strtotime(date("Y", time()) . "-1" . "-1"); //本年开始
            $end_year = strtotime(date("Y", time()) . "-12" . "-31"); //本年结束
            $Paylog = new Paylog();
            $mid = $this->get_mer_mid();
            //平均客单价
            $map['mid'] = ['in', $mid];
            $map['pay_statu'] =['not in','0,8'];
            $total_fee = $Paylog->where($map)->sum('price');
            $total_num = $Paylog->where($map)->count('id');
            $data['average_fee'] = $total_num==0?0:number_format($total_fee / $total_num,2);
            //本月度
            $map1['mid'] = ['in', $mid];
            $map1['pay_statu'] =['not in','0,8'];
            $map1['add_time'] = array(array('egt', $beginThismonth), array('elt', $endThismonth), 'and');
            $month_total_fee = $Paylog->where($map1)->sum('price');
            $data['month_total_fee'] = $month_total_fee;
            //本年度
            $map2['mid'] = ['in', $mid];
            $map2['pay_statu'] =['not in','0,8'];
            $map2['add_time'] = array(array('egt', $begin_year), array('elt', $end_year), 'and');
            $year_total_fee = $Paylog->where($map2)->sum('price');
            $data['year_total_fee'] = $year_total_fee;
            //END
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['data']['item'] = $data;
            return json($re_param);
        }
    }

    //卡券概览
    public function cards_overview(Request $request){
        if ($request->isAjax()) {
            //获取本月起始时间戳和结束时间戳
//            $beginThismonth = mktime(0, 0, 0, date('m'), 1, date('Y'));
//            $endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
            //获取本年度起始时间戳和结束时间戳
//            $begin_year = strtotime(date("Y", time()) . "-1" . "-1"); //本年开始
//            $end_year = strtotime(date("Y", time()) . "-12" . "-31"); //本年结束
            $param = $request->post();
            $start_day = strtotime($param['start_day']);
            $end_day = strtotime($param['end_day']) + 86399;

            $CardUserGet = new CardUserGet();
            $CardConsume = new CardConsume();
            $CardPaycardlog = new CardPaycardlog();
            $Paylog = new Paylog();
            $Merchant = new Merchant();
            $mid = $this->get_mer_mid();
            $top_mid = Session::get('login_top_mid');
            $mids = $Merchant->where(['top_mid' => $top_mid])->column('id');
            //领取
            $map['top_mid'] = $top_mid;
            $map['create_time'] = array(array('egt', $start_day), array('elt', $end_day), 'and');
            $map['card_type'] = ['neq', 'MEMBER_CARD'];
            $data['get_num'] = $CardUserGet->where($map)->count();
            //END
            //核销
            $map1['mid'] = ['in', $mids];
            $map1['create_time'] = array(array('egt', $start_day), array('elt', $end_day), 'and');
            $data['con_num'] = $CardConsume->where($map1)->count();
            //END
            //购买数量
            $map2['mid'] = ['in', $mids];
            $map2['paystatu'] = 1;
            $map2['paytime'] = array(array('egt', $start_day), array('elt', $end_day), 'and');
            $data['pay_num'] = $CardPaycardlog->where($map2)->count();
            //END
            //购买金额
            $data['pay_fee'] = $CardPaycardlog->where($map2)->sum('price');
            //END
            //核销金额
            $map3['mid'] = ['in', $mids];
            $map3['pay_statu'] = 1;
            $map3['add_time'] = array(array('egt', $start_day), array('elt', $end_day), 'and');
            $data['con_fee'] = $Paylog->where($map3)->sum('gel_card_zk');
            //END
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['data']['item'] = $data;
            return json($re_param);
        }
    }

    //会员消费排行
    public function member_payranking(Request $request){
        $Paylog = new Paylog();
        $CardUserGet = new CardUserGet();
        $mid = $this->get_mer_mid();
        if ($mid) {
            $map['mid'] = ['in', $mid];
        }else{
            $map['top_mid'] = Session::get('login_top_mid');
        }
//        $map['pay_statu'] = 1;
//        $map['card_num'] = ['neq',""];
        $map['card_type'] = 'MEMBER_CARD';
        $list = $CardUserGet
            ->field('use_balance,nickname')
            ->where($map)->order('use_balance desc')->limit(0,8)->select();
//        $re_param['$map'] = $map;
//        $re_param['$map-sql'] = $CardUserGet->getLastSql();
        foreach ($list as $k=>$v){
            $list[$k]['sort'] = $k+1;
            $list[$k]['price'] = $v['use_balance'];
            $list[$k]['nickname'] = empty($v['nickname'])?"":$v['nickname'];
        }
        $re_param['return_code'] = "SUCCESS";
        $re_param['return_msg'] = "成功";
        $re_param['data']['item'] = $list;
        return json($re_param);

    }

    //线型图
    public function turnover_statis(){
        $beginWeek = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y'));
        $endWeek = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y'));
        //END
        $weekdata = $this->get_week();
        $mid = $this->get_mer_mid();
        $top_mid = Session::get('login_top_mid');
        //END
        $Paylog = new Paylog();
        $CardUserGet = new CardUserGet();
        $week = [];
        foreach ($weekdata as $k=>$v){
            $map1['add_time'] = array(array('egt', $v['time']), array('elt', $v['time']+86399), 'and');
//            $map1['pay_statu'] = 1;
            $map1['mid'] = ['in',$mid];
            $map1['pay_statu'] = ['not in','0,8'];
            $map1['pay_cate'] = ['neq', 'recharge'];
            $total_yingyenum = $Paylog->where($map1)->sum('price');
            $total_ordernum = $Paylog->where($map1)->count('id');
            $list[$k]['total_yingyenum'] = !empty($total_yingyenum) ? $total_yingyenum:0;
            $list[$k]['total_ordernum'] = !empty($total_ordernum) ? $total_ordernum:0;

            //END
            $map2['create_time'] = array(array('egt', $v['time']), array('elt', $v['time']+86399), 'and');
            $map2['card_type'] = 'MEMBER_CARD';
            $map2['top_mid'] = $top_mid;
            $total_recusernum = $CardUserGet->where($map2)->count('id');
            $list[$k]['total_recusernum'] = !empty($total_recusernum) ? $total_recusernum:0;

        }

        $series[0]['name'] = "营业额";
        $series[0]['type'] = "bar";
        $series[0]['stack'] = "总量";
        $series[0]['data'] = array_column($list, "total_yingyenum");
        //END
        $series[1]['name'] = "订单数";
        $series[1]['type'] = "bar";
        $series[1]['stack'] = "总量";
        $series[1]['data'] = array_column($list, "total_ordernum");
        //END
        $series[2]['name'] = "新增会员";
        $series[2]['type'] = "bar";
        $series[2]['stack'] = "总量";
        $series[2]['data'] = array_column($list, "total_recusernum");
        //END
        $re_data['title'] = array('营业额', '订单数', '新增会员');
        $re_data['year'] = $week;
        $re_data['series'] = $series;

        //税收
        $re_param['return_code'] = "SUCCESS";
        $re_param['return_msg'] = "获取成功";
        $re_param['data']['items']['mychart'] = $re_data;
        return $re_param;
    }


    //本周日期
    public function get_week($time = '', $format='Y-m-d'){
        $time = $time != '' ? $time : time();
        //获取当前周几
        $week = date('w', $time);
        $weekname = array('周一','周二','周三','周四','周五','周六','周日');
        //星期日排到末位
        if(empty($week)){
            $week=7;
        }
        $date = [];
        for ($i=0; $i<7; $i++){
            $date_time = date($format ,strtotime( '+' . $i+1-$week .' days', $time));
            $date[$i]['date'] = $date_time;
            $date[$i]['time'] = strtotime($date_time);
            $date[$i]['week'] = $weekname[$i];
        }
        return $date;
    }



}
